﻿Public Class FileWriter
    Dim nonPremium As String = "     Non-Premium:"
    Dim premium As String = "     Premium:"
    Dim light As String = "          Light:"
    Dim medium As String = "          Medium:"
    Dim heavy As String = "          Heavy:"
    Dim td As String = "          TD:"
    Dim spg As String = "          SPG:"
    Dim tankspaces As String = "               "

    Public Sub Write()
        Dim sb As New System.Text.StringBuilder
        If Main.clboxChinaLight.CheckedItems.Count <> 0 Or Main.clboxChinaMedium.CheckedItems.Count <> 0 Or Main.clboxChinaHeavy.CheckedItems.Count <> 0 Or Main.clboxChinaPremium.CheckedItems.Count <> 0 Then
            sb.AppendLine("China:")
            If Main.clboxChinaLight.CheckedItems.Count <> 0 Or Main.clboxChinaMedium.CheckedItems.Count <> 0 Or Main.clboxChinaHeavy.CheckedItems.Count <> 0 Or Main.clboxChinaPremium.CheckedItems.Count <> 0 Then
                sb.AppendLine(nonPremium)
                If Main.clboxChinaLight.CheckedItems.Count <> 0 Then
                    sb.AppendLine(light)
                    For Each item In Main.clboxChinaLight.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxChinaMedium.CheckedItems.Count <> 0 Then
                    sb.AppendLine(medium)
                    For Each item In Main.clboxChinaMedium.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxChinaHeavy.CheckedItems.Count <> 0 Then
                    sb.AppendLine(heavy)
                    For Each item In Main.clboxChinaHeavy.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
            End If
            If Main.clboxChinaPremium.CheckedItems.Count <> 0 Then
                sb.AppendLine(premium)
                For Each item In Main.clboxChinaPremium.CheckedItems()
                    sb.AppendLine("          " & item)
                Next
            End If
        End If

        If Main.clboxFrenchLight.CheckedItems.Count <> 0 Or Main.clboxFrenchMedium.CheckedItems.Count <> 0 Or Main.clboxFrenchHeavy.CheckedItems.Count <> 0 Or Main.clboxFrenchPremium.CheckedItems.Count <> 0 Or Main.clboxFrenchTD.CheckedItems.Count <> 0 Or Main.clboxFrenchSPG.CheckedItems.Count <> 0 Then
            sb.AppendLine("French:")
            If Main.clboxFrenchLight.CheckedItems.Count <> 0 Or Main.clboxFrenchMedium.CheckedItems.Count <> 0 Or Main.clboxFrenchHeavy.CheckedItems.Count <> 0 Then
                sb.AppendLine(nonPremium)
                If Main.clboxFrenchLight.CheckedItems.Count <> 0 Then
                    sb.AppendLine(light)
                    For Each item In Main.clboxFrenchLight.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxFrenchMedium.CheckedItems.Count <> 0 Then
                    sb.AppendLine(medium)
                    For Each item In Main.clboxFrenchMedium.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxFrenchHeavy.CheckedItems.Count <> 0 Then
                    sb.AppendLine(heavy)
                    For Each item In Main.clboxFrenchHeavy.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxFrenchTD.CheckedItems.Count <> 0 Then
                    sb.AppendLine(td)
                    For Each item In Main.clboxFrenchTD.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxFrenchSPG.CheckedItems.Count <> 0 Then
                    sb.AppendLine(spg)
                    For Each item In Main.clboxFrenchSPG.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
            End If
            If Main.clboxFrenchPremium.CheckedItems.Count <> 0 Then
                sb.AppendLine(premium)
                For Each item In Main.clboxFrenchPremium.CheckedItems()
                    sb.AppendLine("          " & item)
                Next
            End If
        End If

        If Main.clboxGermanLight.CheckedItems.Count <> 0 Or Main.clboxGermanMedium.CheckedItems.Count <> 0 Or Main.clboxGermanHeavy.CheckedItems.Count <> 0 Or Main.clboxGermanTD.CheckedItems.Count <> 0 Or Main.clboxGermanSPG.CheckedItems.Count <> 0 Or Main.clboxGermanPremium.CheckedItems.Count <> 0 Then
            sb.AppendLine("German:")
            If Main.clboxGermanLight.CheckedItems.Count <> 0 Or Main.clboxGermanMedium.CheckedItems.Count <> 0 Or Main.clboxGermanHeavy.CheckedItems.Count <> 0 Or Main.clboxGermanTD.CheckedItems.Count <> 0 Or Main.clboxGermanSPG.CheckedItems.Count <> 0 Then
                sb.AppendLine(nonPremium)
                If Main.clboxGermanLight.CheckedItems.Count <> 0 Then
                    sb.AppendLine(light)
                    For Each item In Main.clboxGermanLight.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxGermanMedium.CheckedItems.Count <> 0 Then
                    sb.AppendLine(medium)
                    For Each item In Main.clboxGermanMedium.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxGermanHeavy.CheckedItems.Count <> 0 Then
                    sb.AppendLine(heavy)
                    For Each item In Main.clboxGermanHeavy.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxGermanTD.CheckedItems.Count <> 0 Then
                    sb.AppendLine(td)
                    For Each item In Main.clboxGermanTD.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxGermanSPG.CheckedItems.Count <> 0 Then
                    sb.AppendLine(spg)
                    For Each item In Main.clboxGermanSPG.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
            End If
            If Main.clboxGermanPremium.CheckedItems.Count <> 0 Then
                sb.AppendLine(premium)
                For Each item In Main.clboxGermanPremium.CheckedItems()
                    sb.AppendLine("          " & item)
                Next
            End If
        End If

        If Main.clboxUKLight.CheckedItems.Count <> 0 Or Main.clboxUKMedium.CheckedItems.Count <> 0 Or Main.clboxUKHeavy.CheckedItems.Count <> 0 Or Main.clboxUKPremium.CheckedItems.Count <> 0 Then
            sb.AppendLine("UK")
            If Main.clboxUKLight.CheckedItems.Count <> 0 Or Main.clboxUKMedium.CheckedItems.Count <> 0 Or Main.clboxUKHeavy.CheckedItems.Count <> 0 Then
                If Main.clboxUKLight.CheckedItems.Count <> 0 Then
                    sb.AppendLine(light)
                    For Each item In Main.clboxUKLight.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxUKMedium.CheckedItems.Count <> 0 Then
                    sb.AppendLine(medium)
                    For Each item In Main.clboxUKMedium.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxUKHeavy.CheckedItems.Count <> 0 Then
                    sb.AppendLine(heavy)
                    For Each item In Main.clboxUKHeavy.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxUKTD.CheckedItems.Count <> 0 Then
                	sb.AppendLine(td)
                	For Each item In Main.clboxUKTD.CheckedItems()
                		sb.AppendLine(tankspaces & item)
                	Next
                End If
            End If
            sb.AppendLine(premium)
            For Each item In Main.clboxUKPremium.CheckedItems()
                sb.AppendLine("          " & item)
            Next
        End If

        If Main.clboxUSALight.CheckedItems.Count <> 0 Or Main.clboxUSAMedium.CheckedItems.Count <> 0 Or Main.clboxUSAHeavy.CheckedItems.Count <> 0 Or Main.clboxUSATD.CheckedItems.Count <> 0 Or Main.clboxUSASPG.CheckedItems.Count <> 0 Or Main.clboxUSAPremium.CheckedItems.Count <> 0 Then
            sb.AppendLine("U.S.A.:")
            If Main.clboxUSALight.CheckedItems.Count <> 0 Or Main.clboxUSAMedium.CheckedItems.Count <> 0 Or Main.clboxUSAHeavy.CheckedItems.Count <> 0 Or Main.clboxUSATD.CheckedItems.Count <> 0 Or Main.clboxUSASPG.CheckedItems.Count <> 0 Then
                sb.AppendLine(nonPremium)
                If Main.clboxUSALight.CheckedItems.Count <> 0 Then
                    sb.AppendLine(light)
                    For Each item In Main.clboxUSALight.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxUSAMedium.CheckedItems.Count <> 0 Then
                    sb.AppendLine(medium)
                    For Each item In Main.clboxUSAMedium.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxUSAHeavy.CheckedItems.Count <> 0 Then
                    sb.AppendLine(heavy)
                    For Each item In Main.clboxUSAHeavy.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxUSATD.CheckedItems.Count <> 0 Then
                    sb.AppendLine(td)
                    For Each item In Main.clboxUSATD.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxUSASPG.CheckedItems.Count <> 0 Then
                    sb.AppendLine(spg)
                    For Each item In Main.clboxUSASPG.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
            End If
            If Main.clboxUSAPremium.CheckedItems.Count <> 0 Then
                sb.AppendLine(premium)
                For Each item In Main.clboxUSAPremium.CheckedItems()
                    sb.AppendLine("          " & item)
                Next
            End If
        End If

        If Main.clboxUSSRLight.CheckedItems.Count <> 0 Or Main.clboxUSSRMedium.CheckedItems.Count <> 0 Or Main.clboxUSSRHeavy.CheckedItems.Count <> 0 Or Main.clboxUSSRTD.CheckedItems.Count <> 0 Or Main.clboxUSSRSPG.CheckedItems.Count <> 0 Or Main.clboxUSSRPremium.CheckedItems.Count <> 0 Then
            sb.AppendLine("U.S.S.R.:")
            If Main.clboxUSSRLight.CheckedItems.Count <> 0 Or Main.clboxUSSRMedium.CheckedItems.Count <> 0 Or Main.clboxUSSRHeavy.CheckedItems.Count <> 0 Or Main.clboxUSSRTD.CheckedItems.Count <> 0 Or Main.clboxUSSRSPG.CheckedItems.Count <> 0 Then
                sb.AppendLine(nonPremium)
                If Main.clboxUSSRLight.CheckedItems.Count <> 0 Then
                    sb.AppendLine(light)
                    For Each item In Main.clboxUSSRLight.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxUSSRMedium.CheckedItems.Count <> 0 Then
                    sb.AppendLine(medium)
                    For Each item In Main.clboxUSSRMedium.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxUSSRHeavy.CheckedItems.Count <> 0 Then
                    sb.AppendLine(heavy)
                    For Each item In Main.clboxUSSRHeavy.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxUSSRTD.CheckedItems.Count <> 0 Then
                    sb.AppendLine(td)
                    For Each item In Main.clboxUSSRTD.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
                If Main.clboxUSSRSPG.CheckedItems.Count <> 0 Then
                    sb.AppendLine(spg)
                    For Each item In Main.clboxUSSRSPG.CheckedItems()
                        sb.AppendLine(tankspaces & item)
                    Next
                End If
            End If
            If Main.clboxUSSRPremium.CheckedItems.Count <> 0 Then
                sb.AppendLine(premium)
                For Each item In Main.clboxUSSRPremium.CheckedItems()
                    sb.AppendLine("          " & item)
                Next
            End If
        End If

        If Main.txtPlayerName.Text = "" Then
            MsgBox("Please enter a player name.", MsgBoxStyle.Critical, Title:="Error!")
        Else
            If System.IO.File.Exists(Main.filepath) = True Then
                If System.IO.File.Exists(Main.filepath & ".old") Then
                    System.IO.File.Delete(Main.filepath & ".old")
                End If
                System.IO.File.Copy(Main.filepath, Main.filepath & ".old")
                Dim objWriter As New System.IO.StreamWriter(Main.filepath)
                objWriter.Write(sb.ToString())
                objWriter.Close()
                MsgBox("Player updated.", MsgBoxStyle.Information, Title:="Information")
            Else
                Dim objWriter As New System.IO.StreamWriter(Main.filepath, False)
                objWriter.Write(sb.ToString())
                objWriter.Close()
                MsgBox("New player added.", MsgBoxStyle.Information, Title:="Information")
            End If
        End If
    End Sub
End Class
